package com.dingwen.treasure.manage.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dingwen.treasure.base.pojo.vo.Result;
import com.dingwen.treasure.manage.dto.SysUserDto;
import com.dingwen.treasure.manage.vo.SysUserVo;
import com.dingwen.treasure.model.entity.mybatisplus.SysUser;
import com.dingwen.treasure.mybatisplus.controller.AbstractBaseControllerMybatisPlusImpl;
import com.dingwen.treasure.mybatisplus.service.SysUserService;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统用户控制器
 *
 * @author dingwen
 * @date 2022/05/08
 */
@Api(tags = "系统用户,基于mybatis-plus封装BaseController")
@ApiSupport(author = "dingwen")
@Slf4j
@RestController
@RequestMapping("sys/user")
public class SysUserController extends AbstractBaseControllerMybatisPlusImpl<SysUser, SysUserDto, SysUserVo> {

    /**
     * 用户服务
     */
    @Resource
    private SysUserService userService;


    @ApiOperation("根据账号查询单个用户")
    @ApiImplicitParam(name = "account", value = "账号", paramType = "path")
    @GetMapping("/feign/{account}")
    public Result<SysUser> getOneByAccount(@PathVariable String account) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getAccount, account);
        SysUser sysUser = userService.getOne(wrapper);
        return success(sysUser);
    }

    /**
     * //TODO postgresql 的问题 or mybatisPlus 完成了转换
     *
     * @param sysUserDTO
     * @return Result
     */
    @ApiOperation("localDataTime测试案列")
    @ApiImplicitParam(name = "sysUserDTO", value = "系统用户数据传输对象", paramType = "body")
    @PostMapping("localDataTime")
    public Result<List<SysUser>> testLocalDateTime(@RequestBody SysUserDto sysUserDTO) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(SysUser::getLastLoginTime, sysUserDTO.getLastLoginTimeStart(),
                sysUserDTO.getLastLoginTimeEnd());
        List<SysUser> sysUsers = userService.list(wrapper);
        return success(sysUsers);
    }


    /**
     * bean转换测试
     *
     * @param sysUserDTO 系统用户dto
     * @return {@link Result}<{@link SysUserVo}>
     */
    @ApiOperation("Bean优雅转换测试")
    @PostMapping("/convert")
    public Result<SysUserVo> beanConvertTest(@RequestBody SysUserDto sysUserDTO) {

        SysUser sysUser = sysUserDTO.dtoToPo(sysUserDTO);
        log.info("DTO ---> PO:  {}", JSONUtil.toJsonStr(sysUser));

        SysUserDto sysUserDtoForBO = sysUserDTO.poToDto(sysUser);
        log.info("PO ---> DTO:  {}", JSONUtil.toJsonStr(sysUserDtoForBO));

        SysUserVo sysUserVO = sysUserDTO.poToVo(sysUser);
        log.info("PO ---> VO:  {}", JSONUtil.toJsonStr(sysUserVO));

        return success(sysUserVO);
    }

}
